<script setup>
import { ref } from 'vue'
import { userEditService } from '@/api/user'
import userStore from '@/store/user'
import { ElMessage } from 'element-plus'

// 表单数据
const form = ref({
  oldPassword: '',
  oldPasswordConfirm: '',
  newPassword: '',
  newPasswordConfirm: ''
})

// 表单引用（用于触发表单验证）
const formRef = ref()

// 表单验证规则
const rules = {
  oldPassword: [
    { required: true, message: '请输入旧密码', trigger: 'blur' },
    { min: 6, max: 18, message: '密码长度在6-18个字符', trigger: 'blur' }
  ],
  oldPasswordConfirm: [
    { required: true, message: '请确认旧密码', trigger: 'blur' },
    { validator: (rule, value, callback) => {
      if (value !== form.value.oldPassword) {
        callback(new Error('两次输入的旧密码不一致'))
      } else {
        callback()
      }
    }, trigger: 'blur' }
  ],
  newPassword: [
    { required: true, message: '请输入新密码', trigger: 'blur' },
    { min: 6, max: 18, message: '密码长度在6-18个字符', trigger: 'blur' }
  ],
  newPasswordConfirm: [
    { required: true, message: '请确认新密码', trigger: 'blur' },
    { validator: (rule, value, callback) => {
      if (value !== form.value.newPassword) {
        callback(new Error('两次输入的新密码不一致'))
      } else {
        callback()
      }
    }, trigger: 'blur' }
  ]
}

// 提交修改
const onSubmit = async () => {
  // 表单验证
  const valid = await formRef.value.validate()
  if (!valid) return

  try {
    // 调用修改接口
    await userEditService({
      id: userStore.userId, // 假设store中有userId
      password: form.value.newPassword
    })
    
    ElMessage.success('密码修改成功')
    // 清空表单
    formRef.value.resetFields()
  } catch (error) {
    ElMessage.error(error.response?.data?.message || '修改失败')
  }
}
</script>

<template>
  <el-card class="page-container">
    <template #header>
      <div class="header">
        <span>密码修改</span>
      </div>
    </template>
    <el-row>
      <el-col :span="12">
        <el-form 
          ref="formRef" 
          :model="form" 
          :rules="rules" 
          label-width="130px" 
          size="large"
        >
          <el-form-item label="请输入旧密码：" prop="oldPassword">
            <el-input 
              v-model="form.oldPassword" 
              type="password" 

              show-password
            ></el-input>
          </el-form-item>
          <el-form-item label="请确认旧密码：" prop="oldPasswordConfirm">
            <el-input 
              v-model="form.oldPasswordConfirm" 
              type="password" 
              show-password
            ></el-input>
          </el-form-item>
          <el-form-item label="请输入新密码：" prop="newPassword">
            <el-input 
              v-model="form.newPassword" 
              type="password" 
              show-password
            ></el-input>
          </el-form-item>
          <el-form-item label="请确认新密码：" prop="newPasswordConfirm">
            <el-input 
              v-model="form.newPasswordConfirm" 
              type="password" 
              show-password
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit">修改密码</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </el-card>
</template>